<template>
    <div>
        <h2>机构添加</h2>

        <table class="table table-bordered">
            <tbody>

                <tr>
                    <td align="right">机构名称</td>
                    <td><input type="text" v-model="info.Name"/></td>
                </tr>

                <tr>
                    <td align="right">机构封面</td>
                    <td>
                        <input type="file" @change="img"/>
                        <img :src="info.Photo" width="100" height="100" v-if="info.Photo != ''"/>
                    </td>
                </tr>

                <tr>
                    <td align="right">特色服务</td>
                    <td>
                        <select v-model="info.Type">
                            <option value="1">请选择</option>
                            <option>24小时监护</option>
                            <option>专属医师</option>
                            <option>特殊护理</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td align="right">营业时间</td>
                    <td><input type="date" v-model="info.StareTime"/></td>
                </tr>

                <tr>
                    <td align="right">机构地址</td>
                    <td><input type="text" v-model="info.Addr"/></td>
                </tr>

                <tr>
                    <td align="right">联系电话</td>
                    <td><input type="text" v-model="info.Phone" maxlength="11"/></td>
                </tr>

                <tr>
                    <td align="right">发布时间</td>
                    <td>
                        <input type="radio" value="立即发布" v-model="info.OpenTime"/>立即发布
                        <input type="radio" value="定时发布" v-model="info.OpenTime"/>定时发布

                        <br>
                        <input type="date" v-model="info.Time"/>
                    </td>
                </tr>



                <tr>
                    <td align="right">机构详情</td>
                    <td>
                        <textarea v-model="info.Desc"></textarea>
                    </td>
                </tr>

                <tr>
                    <td align="right">机构状态</td>
                    <td>
                        <input type="radio" value="1" v-model="info.Stare"/>已发布
                        <input type="radio" value="0" v-model="info.Stare"/>未发布
                    </td>
                </tr>

                <tr>
                    <td align="right"></td>
                    <td><input type="button" value="保存" class="btn btn-info" @click="ok"/></td>
                </tr>

            </tbody>
        </table>

    </div>
</template>

<script setup lang="ts">
import {ref,reactive} from 'vue'
import axios from 'axios';
import { useRouter,useRoute } from 'vue-router';
import {useUserStore} from '@/stores/user'

let router = useRouter();
let route = useRoute();


let info:any=reactive({
    Name: '',
  Photo: '',
  Type: 1,
  StareTime:'',
  Addr: '',
  Phone: '',
  OpenTime: '',
  Time: '',
  Desc: '',
  Stare: 1,
})


const img = (e:any)=>{
    console.log(e)
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file',obj);
    if(f)
    {
        axios({
            url:'http://localhost:5277/api/Img/Imgs',
            method:'post',
            data:f
        })
        .then((res)=>{
            if(res.data == '图片大小不合适')
            {
                alert('图片格式或大小不正确');
                return;
            }
            else
            {
                info.Photo = res.data;
            }
        })
        .catch((err)=>{
            console.log(err);
        })
    }
}


const ok = ()=>{
    if(info.Name == "")
    {
        alert('机构名称不能为空');
        return;
    }
    if(info.Photo == "")
    {
        alert('机构封面不能为空');
        return;
    }
    if(info.Type == "")
    {
        alert('特色服务不能为空');
        return;
    }
    if(info.Addr == "")
    {
        alert('机构地址不能为空');
        return;
    }
    if(info.Phone == "")
    {
        alert('联系电话不能为空');
        return;
    }
    if(info.Desc == "")
    {
        alert('机构详情不能为空');
        return;
    }
    axios({
        url:'http://localhost:5277/api/Gou/AddGou',
        method:'post',
        data:info
    })
    .then((res)=>{
        if(res.data == -1)
        {
            alert('机构名称重复');
            return;
        }
        else if(res.data > 0)
        {
            alert('保存成功');
            router.push('/goushow')
            return;
        }
        else
        {
            alert('保存失败')
        }
    })
    .catch((err)=>{
        console.log(err);
    })
}







</script>

<style scoped>

</style>